package com.mec.offload.controller;


import com.alibaba.fastjson.JSONObject;
import com.mec.offload.model.DistanceInfo;
import com.mec.offload.model.NodeTerm;
import com.mec.offload.service.DistanceInfoService;
import com.mec.offload.service.MecService;
import com.mec.offload.service.NodeTermService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/offload/NodeTerm")
@CrossOrigin
public class NodeTermController{
    @Autowired
    private NodeTermService nodeTermService;
    @Autowired
    private DistanceInfoService distanceInfoService;
    @Autowired
    private MecService mecService;
    //@CrossOrigin(origins = "*")
    @RequestMapping("/getAllNodeTerm")
    @ResponseBody
    public List<NodeTerm> getAllNode_term(){
        try {
            nodeTermService.getAllNodeTerm();
            for(int i=0;i<nodeTermService.getAllNodeTerm().size();i++)
                System.out.print(nodeTermService.getAllNodeTerm().get(i).getTermId());
            return nodeTermService.getAllNodeTerm();
        }catch (Exception e){
            e.printStackTrace();
        }
        System.out.println("error");
        return null;
    }
    //@CrossOrigin(origins = "*")
    @RequestMapping("/getAllNodeTerm1")
    @ResponseBody
    public String getAllNode_term1( @RequestParam(required=false,defaultValue="1") int page,
                                    @RequestParam(required=false,defaultValue="15") int limit
    ){
        try {
            nodeTermService.getAllNodeTerm();
            for(int i=0;i<nodeTermService.getAllNodeTerm().size();i++)
                System.out.print(nodeTermService.getAllNodeTerm().get(i).getTermId());
            List<NodeTerm> data=nodeTermService.queryAllDataFromTable(page,limit);
            int countx=  nodeTermService.queryAllCount();

            JSONObject jo = new JSONObject();
            jo.put("code",0);
            jo.put("msg","");
            jo.put("count",countx);
            jo.put("data", data);
            return jo.toString();
            //return nodeTermService.getAllNodeTerm();
        }catch (Exception e){
            e.printStackTrace();
        }
        System.out.println("error");
        return null;
    }
    //@CrossOrigin(origins = "*")
    @RequestMapping("/Register")
    @ResponseBody
    public int register_mec(int ID)
    {
        try {
            Integer mecID=0;
            List<DistanceInfo> listMEC = distanceInfoService.getDistanceInfoByTermId(ID);
            if(listMEC.size()>=1)
                mecID=listMEC.get(0).getMecId();
            else
                return 0;
            mecService.updateMec(mecID);
            NodeTerm node=new NodeTerm();
            node.setLoginMecId(mecID);
            node.setTermId(ID);
            nodeTermService.updateNodeTerm(node);
            return 1;
        }catch (Exception e)
        {
            e.printStackTrace();
            return 0;
        }
    }

}
